package com.xyht.medicine_management_system.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class TimeUtil {
    /*
     * 前端事件显示的默认格式
     */
    public final static String DefDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    public final static String DefDateSimpleTimeFormat = "yyyy-MM-dd";
    public final static String DefTimeFormat = "HH:mm";
    public final static String DefTimeAndSenFormat = "HH:mm:ss";

    /**
     * 将时间格式化成字符串
     *
     * @param date:输入的事件
     * @return 返回格式华的时间字符串
     */
    public static String Dateformat(Date date) {
        SimpleDateFormat fromat = new SimpleDateFormat(DefDateTimeFormat);
        return fromat.format(date);
    }

    public static String DateformatTimestamp(long date) {
        SimpleDateFormat fromat = new SimpleDateFormat(DefDateTimeFormat);
        return fromat.format(date);
    }

    public static String WXDateformat(Date date) {
        SimpleDateFormat fromat = new SimpleDateFormat(DefDateTimeFormat);
        String formatData = fromat.format(date);
        String wxFormatData = formatData.replace(" ", "T");
        return wxFormatData + "+08:00";
    }

    public static String DateHanZiSimpleTimestamp(long date) {
        SimpleDateFormat fromat = new SimpleDateFormat(DefDateSimpleTimeFormat);
        StringBuffer stringBuffer = new StringBuffer(fromat.format(new Date(date)));
        stringBuffer.replace(4, 5, "年");
        stringBuffer.replace(7, 8, "月");
        stringBuffer.append("日");
        return stringBuffer.toString();
    }

    public static String DateSimpleformat(Date date) {
        SimpleDateFormat fromat = new SimpleDateFormat("yyyy-MM-dd");
        return fromat.format(date);
    }

    public static String DateHanZiSimpleFormat(Date date) {
        SimpleDateFormat fromat = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer stringBuffer = new StringBuffer(fromat.format(date));
        stringBuffer.replace(4, 5, "年");
        stringBuffer.replace(7, 8, "月");
        stringBuffer.append("日");
        return stringBuffer.toString();
    }

    public static String getTimeStamp() {
        long time = new Date().getTime();
        return String.valueOf(time);
    }

    public static String TimeFormatSting(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DefTimeFormat);
        return format.format(date);
    }


    public static String TimeAndSecFormat(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DefTimeAndSenFormat);
        return format.format(date);
    }

    public static String DateFormatSting(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DefDateSimpleTimeFormat);
        return format.format(date);
    }

    public static String TimeHanZiFormatSting(long date) {
        SimpleDateFormat format = new SimpleDateFormat(DefTimeFormat);
        return format.format(date);
    }


    /**
     * 将时间字符串转为Date
     *
     * @param date_str:格式化的时间字符串
     * @return Date类型的时间
     */
    public static Date StringtoDate(String date_str) {
        if (date_str == null || date_str.length() < 9) {
            return null;
        }
        SimpleDateFormat format;
        if (date_str.length() < 18) {
            format = new SimpleDateFormat(DefDateSimpleTimeFormat);
        } else {
            format = new SimpleDateFormat(DefDateTimeFormat);
        }
        try {
            return format.parse(date_str);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date TimeFormat(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat(DefTimeFormat);
        Date date = new Date();
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将RFC3339标准格式的时间字符串转换为Date类型
     *
     * @param rfc3339DateTime RFC3339标准格式的时间字符串
     * @return Date类型的时间
     */
    public static Date rfc3339ParseDate(String rfc3339DateTime) {
        // 使用预定义的ISO_OFFSET_DATE_TIME格式器解析RFC3339格式的时间字符串为OffsetDateTime对象
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        OffsetDateTime offsetDateTime = OffsetDateTime.parse(rfc3339DateTime, formatter);

        // 将OffsetDateTime对象转换为Instant对象，然后转换为Date对象
        return Date.from(offsetDateTime.toInstant());
    }

    /**
     * 将参考日期偏移指定的偏移量，单位秒
     *
     * @param src    参考日期
     * @param offset 偏移量
     * @return 偏移后的日期
     */
    public static Date AddSeconds(Date src, int offset) {
        if (src == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.SECOND, offset);
        return calendar.getTime();

    }

    public static Date AddMinute(Date src, int offset) {
        if (src == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MINUTE, offset);
        return calendar.getTime();

    }

    /**
     * 将参考日期偏移指定的偏移量，单位天
     *
     * @param src    参考日期
     * @param offset 偏移量
     * @return 偏移后的日期
     */
    public static Date AddDays(Date src, int offset) {
        if (src == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.DATE, offset);
        return calendar.getTime();

    }

    /**
     * 将参考日期偏移指定的偏移量，单位月
     *
     * @param src    参考日期
     * @param offset 偏移量
     * @return 偏移后的日期
     */
    public static Date AddMonths(Date src, int offset) {
        if (src == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MONTH, offset);
        return calendar.getTime();
    }

    /**
     * 将参考日期偏移指定的偏移量，单位年
     *
     * @param src    参考日期
     * @param offset 偏移量
     * @return 偏移后的日期
     */
    public static Date AddYears(Date src, int offset) {
        if (src == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.YEAR, offset);
        return calendar.getTime();
    }

    /**
     * @param src 源时间
     * @param dst 目标时间
     * @return 两者之间的偏移，单位月
     */
    public static int GetDiffMonth(Date src, Date dst) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(src);
        Calendar calendar_dst = Calendar.getInstance();
        calendar_dst.setTime(dst);
        int diff_year = calendar_src.get(Calendar.YEAR) - calendar_dst.get(Calendar.YEAR);
        if (diff_year < 0)
            diff_year *= -1;
        int diff_month = calendar_src.get(Calendar.MONTH) - calendar_dst.get(Calendar.MONTH);
        if (diff_month < 0)
            diff_month *= -1;
        return diff_year * 12 + diff_month;
    }

    /**
     * @param src 源时间
     * @param dst 目标时间
     * @return 两段时间是否跨天
     */
    public static boolean isDiffDay(Date src, Date dst) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(src);
        Calendar calendar_dst = Calendar.getInstance();
        calendar_dst.setTime(dst);
        int diff_year = calendar_src.get(Calendar.YEAR) - calendar_dst.get(Calendar.YEAR);
        if (diff_year != 0)
            return true;
        int diff_month = calendar_src.get(Calendar.MONTH) - calendar_dst.get(Calendar.MONTH);
        if (diff_month != 0)
            return true;
        int diff_day = calendar_src.get(Calendar.DAY_OF_MONTH) - calendar_dst.get(Calendar.DAY_OF_MONTH);
        return diff_day != 0;
    }

    /**
     * @param date 传入日期
     * @return 判断传入日期是否当月最后一天
     */
    public static boolean isLastDayOfMonth(Date date) {
        return AddDays(date, 1).getMonth() == date.getMonth();
    }

    //获取当前日期，以Date的形式返回
    public static Date GetDate() {
        Date srcd = new Date();
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.HOUR, 0);
        calendar_src.set(Calendar.HOUR_OF_DAY, 0);
        calendar_src.set(Calendar.MINUTE, 0);
        calendar_src.set(Calendar.SECOND, 0);
        calendar_src.set(Calendar.MILLISECOND, 0);
        return calendar_src.getTime();
    }

    //传入日期，获取该天0点，以Date的形式返回
    public static Date GetStartDate(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.HOUR, 0);
        calendar_src.set(Calendar.HOUR_OF_DAY, 0);
        calendar_src.set(Calendar.MINUTE, 0);
        calendar_src.set(Calendar.SECOND, 0);
        calendar_src.set(Calendar.MILLISECOND, 0);
        return calendar_src.getTime();
    }

    //传入两个日期，将第一天转为第二天的年月日，以Date的形式返回
    public static Date GetSameDayDate(Date date1, Date date2) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(date1);
        calendar_src.set(Calendar.YEAR, date2.getYear());
        calendar_src.set(Calendar.MONTH, date2.getMonth());
        calendar_src.set(Calendar.DAY_OF_YEAR, date2.getDay());
        return calendar_src.getTime();
    }

    //传入日期，获取该天24点，以Date的形式返回
    public static Date GetEndDate(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.HOUR_OF_DAY, 23);
        calendar_src.set(Calendar.MINUTE, 59);
        calendar_src.set(Calendar.SECOND, 59);
        calendar_src.set(Calendar.MILLISECOND, 999);
        return calendar_src.getTime();
    }

    //传入日期，获取该星期第一天0点，以Date的形式返回
    public static Date GetStartDateOfWeek(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar_src.set(Calendar.HOUR, 0);
        calendar_src.set(Calendar.HOUR_OF_DAY, 0);
        calendar_src.set(Calendar.MINUTE, 0);
        calendar_src.set(Calendar.SECOND, 0);
        calendar_src.set(Calendar.MILLISECOND, 0);
        return calendar_src.getTime();
    }

    //传入日期，获取该星期最后一天24点，以Date的形式返回
    public static Date GetEndDateOfWeek(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        calendar_src.set(Calendar.HOUR_OF_DAY, 23);
        calendar_src.set(Calendar.MINUTE, 59);
        calendar_src.set(Calendar.SECOND, 59);
        calendar_src.set(Calendar.MILLISECOND, 999);
        calendar_src.add(Calendar.DATE,1);
        return calendar_src.getTime();
    }

    //传入日期，获取该月第一天0点，以Date的形式返回
    public static Date GetStartDateOfMonth(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.set(Calendar.DAY_OF_MONTH, 1);
        calendar_src.set(Calendar.HOUR, 0);
        calendar_src.set(Calendar.HOUR_OF_DAY, 0);
        calendar_src.set(Calendar.MINUTE, 0);
        calendar_src.set(Calendar.SECOND, 0);
        calendar_src.set(Calendar.MILLISECOND, 0);
        return calendar_src.getTime();
    }

    //传入日期，获取该月最后一天24点，以Date的形式返回
    public static Date GetEndDateOfMonth(Date srcd) {
        Calendar calendar_src = Calendar.getInstance();
        calendar_src.setTime(srcd);
        calendar_src.add(Calendar.MONTH, 1);
        calendar_src.set(Calendar.DAY_OF_MONTH, 1);
        calendar_src.add(Calendar.DATE, -1);
        calendar_src.set(Calendar.HOUR_OF_DAY, 23);
        calendar_src.set(Calendar.MINUTE, 59);
        calendar_src.set(Calendar.SECOND, 59);
        calendar_src.set(Calendar.MILLISECOND, 999);
        return calendar_src.getTime();
    }


    /**
     * 比较两个字符串时间大小
     */
    public static int compareTwoTime(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        int flagValue = 0;
        try {
            Date date1, date2;
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            long millisecond = date1.getTime() - date2.getTime();
            if (millisecond > 0) {
                flagValue = 1;
            } else if (millisecond < 0) {
                flagValue = -1;
            } else if (millisecond == 0) {
                flagValue = 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (flagValue);
    }

    /**
     * 表两个时间差
     */
    public static int compareTwoTime(Date time1, Date time2) {
        int flagValue = 0;
        try {
            long millisecond = time1.getTime() - time2.getTime();
            if (millisecond > 0) {
                flagValue = 1;
            } else if (millisecond < 0) {
                flagValue = -1;
            } else if (millisecond == 0) {
                flagValue = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (flagValue);
    }

    /**
     * 比较两个时间相差天数
     */
    public static float calculateTimeGapDay(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        float day = 0;
        Date date1 = null;
        Date date2 = null;

        try {
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            long millisecond = date2.getTime() - date1.getTime();
            day = millisecond / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (day);
    }

    /**
     * 比较两个时间相差天数
     */
    public static float calculateTimeGapDay(Date time1, Date time2) {
        float day = 0;
        try {
            Date date1, date2;
            date1 = time1;
            date2 = time2;
            long millisecond = date2.getTime() - date1.getTime();
            day = millisecond / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (day);
    }

    /**
     * 比较两个时间相差小时
     */
    public static double calculatetimeGapHour(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        double hour = 0;
        try {
            Date date1, date2;
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            double millisecond = date2.getTime() - date1.getTime();
            hour = millisecond / (60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hour;
    }

    /**
     * 比较两个时间相差小时
     */
    public static double calculatetimeGapHour(Date date1, Date date2) {
        double hour = 0;
        double millisecond = date2.getTime() - date1.getTime();
        hour = millisecond / (60 * 60 * 1000);
        return hour;
    }

    /**
     * 比较两个时间相差分钟
     */
    public static double calculatetimeGapMinute(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        double minute = 0;
        try {
            Date date1, date2;
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            double millisecond = date2.getTime() - date1.getTime();
            minute = millisecond / (60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return minute;
    }

    /**
     * 比较两个时间相差分钟
     */
    public static double calculatetimeGapMinute(Date date1, Date date2) {
        double minute = 0;
        double millisecond = date2.getTime() - date1.getTime();
        minute = millisecond / (60 * 1000);
        return minute;
    }

    /**
     * 比较两个时间相差秒
     */
    public static double calculatetimeGapSecond(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        double second = 0;
        try {
            Date date1, date2;
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            double millisecond = date2.getTime() - date1.getTime();
            second = millisecond / (1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return second;
    }

    /**
     * 比较两个时间相差秒
     */
    public static double calculatetimeGapSecond(Date date1, Date date2) {
        double second = 0;
        double millisecond = date2.getTime() - date1.getTime();
        second = millisecond / (1000);
        return second;
    }

    public static Date getStandardDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String date = simpleDateFormat.format(d);
        return TimeUtil.StringtoDate(date);
    }

    /**
     * 获取对应日期星期几
     */
    public static String getWeek(Date date) {
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    public static List<Map<String, Object>> getWeekList() {
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < weeks.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", weeks[i]);
            map.put("value", 0);
            list.add(map);
        }
        return list;
    }


}
